/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2003 Ferdinando Ametrano
 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
 Copyright (C) 2004, 2005 StatPro Italia srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file OSRandomVariable.h
    \brief OSRandomVariable.h
*/

#ifndef _OSCountableProbabilitySpace_
#define _OSCountableProbabilitySpace_
#include <src/math/Stochastic/OSProbabilitySpace.h>
#include <set>

//! prototype function for finite indexable random accessible Sample Space.
class Indice_Set : public std::set<OS_size> {
public :
	typedef T						value_type;
	typedef std::set<OS_size>		Base;
	Indice_Set(const & Indice_Set & from) : Base(from) {};
	inline Indice_Set& operator & (const Indice_Set& from) { //beurk
		Base result_;
		iterator it_this;
		for( Indice_Set::const_iterator it( from.begin() ); it != from.end(),it_this = find(*it); ++it ) {
			if (it_this != end())
				result_.insert(it_this);
		}
		*this = result_;
		return *this;
      };
	inline Indice_Set& operator | (const Indice_Set& from) {
		insert(from.begin(),from.end());
		return *this;
     };
	inline Indice_Set& operator = (const Base& from) {
		Base::operator =(from);
		return *this;
     };
};


// Utilities for Vector like sample space
template <class Vector>
class VectorSampleSpace : public SampleSpace<Indice_Set>
{
public:
	typedef typename SampleSpace<Indice_Set>				Base;
	typedef typename Base::value_type						value_type;
	OS_bool in(const & value_type index) {
			return (index >=0 && index < states_.size() );
	}
protected :
	Vector states_;
};

template <class DoubleVector>
struct VectorProbability : public Probability<Indice_Set>
{
public :
		OS_double operator()(const EventSet_type& B, const EventSet_type& A) const //conditional probability
		{	// apply operator+ to operands
			return (*this)::operator (B&A)/(*this)::operator (A);
		}
};


#endif
